package io.renren.controller;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.entity.CustomContractEntity;
import io.renren.service.CustomContractService;
import io.renren.utils.PageUtils;
import io.renren.utils.Query;
import io.renren.utils.R;


/**
 * 客户协议
 * 
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2017-06-15 20:58:29
 */
@RestController
@RequestMapping("customcontract")
public class CustomContractController {

	/**
	 * 日志对象
	 */
	protected Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private CustomContractService customContractService;
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("customcontract:list")
	public R list(@RequestParam Map<String, Object> params){
//		//与b1进行数据同步
		try {
			customContractService.startSynToB1();
		} catch (Exception e) {
			//logger.info("与b1同步发生异常，数据可能不是最新的，请联系系统管理员！");
			logger.error(e.getMessage(),e);
			e.printStackTrace();
		}

		//查询列表数据
        Query query = new Query(params);

		List<CustomContractEntity> customContractList = customContractService.queryList(query);
		int total = customContractService.queryTotal(query);
		
		PageUtils pageUtil = new PageUtils(customContractList, total, query.getLimit(), query.getPage());
		
		return R.ok().put("page", pageUtil);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("customcontract:info")
	public R info(@PathVariable("id") Integer id){
		CustomContractEntity customContract = customContractService.queryObject(id);
		
		return R.ok().put("customContract", customContract);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("customcontract:save")
	public R save(@RequestBody CustomContractEntity customContract){
		customContractService.save(customContract);
		
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("customcontract:update")
	public R update(@RequestBody CustomContractEntity customContract){
		customContractService.update(customContract);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("customcontract:delete")
	public R delete(@RequestBody Integer[] ids){
		customContractService.deleteBatch(ids);
		
		return R.ok();
	}
	
}
